﻿namespace Sunny.Controls
{
	public class NumericRangeSearchItem : SearchItem
	{
		private Telerik.Windows.Controls.RadNumericUpDown _StartNumber;
		protected Telerik.Windows.Controls.RadNumericUpDown StartNumber
		{
			get
			{
				if (this._StartNumber == null)
				{
					this._StartNumber = new Telerik.Windows.Controls.RadNumericUpDown();
					this._StartNumber.NumberFormatInfo = new System.Globalization.NumberFormatInfo() { NumberGroupSeparator = "" };
					this._StartNumber.NumberDecimalDigits = 0;
					this._StartNumber.Width = 50;
					this._StartNumber.Value = null;
				}
				return this._StartNumber;
			}
		}

		private Telerik.Windows.Controls.RadNumericUpDown _EndNumber;
		protected Telerik.Windows.Controls.RadNumericUpDown EndNumber
		{
			get
			{
				if (this._EndNumber == null)
				{
					this._EndNumber = new Telerik.Windows.Controls.RadNumericUpDown();
					this._EndNumber.NumberFormatInfo = new System.Globalization.NumberFormatInfo() { NumberGroupSeparator = "" };
					this._EndNumber.NumberDecimalDigits = 0;
					this._EndNumber.Width = 50;
					this._EndNumber.Value = null;
				}
				return this._EndNumber;
			}
		}

		public int NumberDecimalDigits
		{
			get
			{
				return ((Telerik.Windows.Controls.RadNumericUpDown)this.StartNumber).NumberDecimalDigits;
			}
			set
			{
				((Telerik.Windows.Controls.RadNumericUpDown)this.StartNumber).NumberDecimalDigits = value;
				((Telerik.Windows.Controls.RadNumericUpDown)this.EndNumber).NumberDecimalDigits = value;
			}
		}

		public bool ShowButtons
		{
			get
			{
				return ((Telerik.Windows.Controls.RadNumericUpDown)this.StartNumber).ShowButtons;
			}
			set
			{
				((Telerik.Windows.Controls.RadNumericUpDown)this.StartNumber).ShowButtons = value;
				((Telerik.Windows.Controls.RadNumericUpDown)this.EndNumber).ShowButtons = value;
			}
		}

		private System.Windows.Controls.Grid _SimpleSearcherInput;
		public override System.Windows.FrameworkElement SimpleSearcherInput
		{
			get
			{
				if (this._SimpleSearcherInput == null)
				{
					this._SimpleSearcherInput = new System.Windows.Controls.Grid();
					this._SimpleSearcherInput.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition());
					this._SimpleSearcherInput.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition());
					this._SimpleSearcherInput.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition());

					this._SimpleSearcherInput.Children.Add(this.StartNumber);

					System.Windows.Controls.TextBlock text = new System.Windows.Controls.TextBlock { Text = "至" };
					this._SimpleSearcherInput.Children.Add(text);
					System.Windows.Controls.Grid.SetColumn(text, 1);

					this._SimpleSearcherInput.Children.Add(this.EndNumber);
					System.Windows.Controls.Grid.SetColumn(this.EndNumber, 2);
				}
				return this._SimpleSearcherInput;
			}
		}

		/*
	{ Text: "包含", Value: "{0}.Contains({1})" },
	{ Text: "不包含", Value: "!{0}.Contains({1})" },
	{ Text: "等于", Value: "{0}=={1}" },
	{ Text: "始于", Value: "{0}.StartsWith({1})" },
	{ Text: "非始于", Value: "!{0}.StartsWith({1})" },
	{ Text: "终于", Value: "{0}.EndsWith({1})" },
	{ Text: "非终于", Value: "!{0}.EndsWith({1})" }
		 */
		private Operator[] _OperatorOptions;
		public override Operator[] Operators
		{
			get
			{
				if (this._OperatorOptions == null)
					this._OperatorOptions = new Operator[]
					{
						new Operator(">","{0}>{1}"),
						new Operator("≥","{0}>={1}"),
						new Operator("<","{0}<{1}"),
						new Operator("≤","{0}<={1}"),
						new Operator("=","{0}={1}"),
						new Operator("≠","{0}!={1}")
					};
				return this._OperatorOptions;
			}
		}

		public override System.Windows.FrameworkElement AdvanceSearcherInput
		{
			get
			{
				return new Telerik.Windows.Controls.RadNumericUpDown { NumberDecimalDigits = 0 };
			}
		}

		public override Searcher.WhereCommand GetCommand(string field, int index)
		{
			string command = null;
			System.Collections.ArrayList parameters = new System.Collections.ArrayList();
			if (this.StartNumber.Value != null)
			{
				command = string.Format("{0}>=@{1}", field, index + parameters.Count);
				parameters.Add((decimal?)this.StartNumber.Value);
			}
			if (this.EndNumber.Value != null)
			{
				if (command != null)
					command += " and ";
				command += string.Format("{0}<=@{1}", field, index + parameters.Count);
				parameters.Add((decimal?)this.EndNumber.Value);
			}
			if (parameters.Count == 0)
				return null;
			return new Sunny.Controls.Searcher.WhereCommand { Text = command, Parameters = parameters.ToArray() };
		}

		public override void Empty()
		{
			this.StartNumber.Value = null;
			this.EndNumber.Value = null;
		}
	}
	//public class DoubleRangeSearchItem : SearchItem
	//{
	//	private Syncfusion.Windows.Shared.DoubleTextBox _StartNumber;
	//	protected Syncfusion.Windows.Shared.DoubleTextBox StartNumber
	//	{
	//		get
	//		{
	//			if (this._StartNumber == null)
	//			{
	//				this._StartNumber = new Syncfusion.Windows.Shared.DoubleTextBox();
	//				//this._StartNumber.Width = 50;
	//			}
	//			return this._StartNumber;
	//		}
	//	}

	//	private Syncfusion.Windows.Shared.DoubleTextBox _EndNumber;
	//	protected Syncfusion.Windows.Shared.DoubleTextBox EndNumber
	//	{
	//		get
	//		{
	//			if (this._EndNumber == null)
	//			{
	//				this._EndNumber = new Syncfusion.Windows.Shared.DoubleTextBox();
	//				this._EndNumber.Width = 50;
	//			}
	//			return this._EndNumber;
	//		}
	//	}

	//	private System.Windows.Controls.Grid _SimpleSearcherInput;
	//	public override System.Windows.FrameworkElement SimpleSearcherInput
	//	{
	//		get
	//		{
	//			if (this._SimpleSearcherInput == null)
	//			{
	//				this._SimpleSearcherInput = new System.Windows.Controls.Grid();
	//				this._SimpleSearcherInput.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition());
	//				this._SimpleSearcherInput.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition());
	//				this._SimpleSearcherInput.ColumnDefinitions.Add(new System.Windows.Controls.ColumnDefinition());

	//				this._SimpleSearcherInput.Children.Add(this.StartNumber);

	//				System.Windows.Controls.TextBlock text = new System.Windows.Controls.TextBlock { Text = "至" };
	//				this._SimpleSearcherInput.Children.Add(text);
	//				System.Windows.Controls.Grid.SetColumn(text, 1);

	//				this._SimpleSearcherInput.Children.Add(this.EndNumber);
	//				System.Windows.Controls.Grid.SetColumn(this.EndNumber, 2);
	//			}
	//			return this._SimpleSearcherInput;
	//		}
	//	}

	//	/*
	//{ Text: "包含", Value: "{0}.Contains({1})" },
	//{ Text: "不包含", Value: "!{0}.Contains({1})" },
	//{ Text: "等于", Value: "{0}=={1}" },
	//{ Text: "始于", Value: "{0}.StartsWith({1})" },
	//{ Text: "非始于", Value: "!{0}.StartsWith({1})" },
	//{ Text: "终于", Value: "{0}.EndsWith({1})" },
	//{ Text: "非终于", Value: "!{0}.EndsWith({1})" }
	//	 */
	//	private Operator[] _OperatorOptions;
	//	public override Operator[] Operators
	//	{
	//		get
	//		{
	//			if (this._OperatorOptions == null)
	//				this._OperatorOptions = new Operator[]
	//				{
	//					new Operator(">","{0}>{1}"),
	//					new Operator("≥","{0}>={1}"),
	//					new Operator("<","{0}<{1}"),
	//					new Operator("≤","{0}<={1}"),
	//					new Operator("=","{0}={1}"),
	//					new Operator("≠","{0}!={1}")
	//				};
	//			return this._OperatorOptions;
	//		}
	//	}

	//	public override System.Windows.FrameworkElement AdvanceSearcherInput
	//	{
	//		get
	//		{
	//			return new Syncfusion.Windows.Shared.DoubleTextBox();
	//		}
	//	}

	//	public override Searcher.WhereCommand GetCommand(string field, int index)
	//	{
	//		string command = null;
	//		System.Collections.ArrayList parameters = new System.Collections.ArrayList();
	//		if (this.StartNumber.Value != null)
	//		{
	//			command = string.Format("{0}>=@{1}", field, index + parameters.Count);
	//			parameters.Add(this.StartNumber.Value);
	//		}
	//		if (this.EndNumber.Value != null)
	//		{
	//			if (command != null)
	//				command += " and ";
	//			command += string.Format("{0}<=@{1}", field, index + parameters.Count);
	//			parameters.Add(this.EndNumber.Value);
	//		}
	//		if (parameters.Count == 0)
	//			return null;
	//		return new Sunny.Controls.Searcher.WhereCommand { Text = command, Parameters = parameters.ToArray() };
	//	}

	//	public override void Empty()
	//	{
	//		this.StartNumber.Value = null;
	//		this.EndNumber.Value = null;
	//	}
	//}
}